package com.mooral.web.action;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;

public class MooralActionExceptionHandler extends ExceptionHandler {

    public ActionForward execute(Exception ex, ExceptionConfig ae, ActionMapping mapping,
	    ActionForm formInstance, HttpServletRequest request, HttpServletResponse response)
	    throws ServletException {
	ActionForward forward = super.execute(ex, ae, mapping, formInstance, request, response);
	ActionMessage error = null;
	String property = null;
	String msg = "";
	if (ex instanceof RuntimeException)
	    msg = ((RuntimeException) ex).getMessage();
	else
	    msg = ex.getMessage();
	error = new ActionMessage("errors.detail", msg);
	property = error.getKey();
	storeException(request, property, error, forward, "request");
	return forward;
    }

    /*
     * public ActionForward execute( Exception ex, ExceptionConfig exConfig,
     * ActionMapping mapping, ActionForm form, HttpServletRequest request,
     * HttpServletResponse response ) throws ServletException{
     * ActionError error = new ActionError (exConfig.getKey (), ex.getMessage
     * ());
     * ActionForward forward = mapping.getInputForward(); String property =
     * ActionErrors.GLOBAL_MESSAGE; String scope = exConfig.getScope();
     * storeException (request, property, error, forward, scope);
     * ex.printStackTrace (System.err); // should log request.setAttribute
     * ("ex", ex); //return forward;
     * return (new ActionForward(mapping.getInput())); //return new
     * ActionForward(exConfig.getPath()); }
     */
}
